package com.css.fxfzxczh.modules.instrumentIntensity.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.instrumentIntensity.entity.InstrumentIntensity;
import com.css.fxfzxczh.modules.instrumentIntensity.repository.InstrumentIntensityRepository;
import com.css.fxfzxczh.modules.instrumentIntensity.repository.impl.InstrumentIntensityRepositoryImpl;
import com.css.fxfzxczh.modules.instrumentIntensity.service.InstrumentIntensityService;

import com.css.fxfzxczh.modules.instrumentIntensity.verify.InstrumentIntensityExcelVerifyHandler;
import com.css.fxfzxczh.modules.instrumentIntensity.vo.ImportExcelVo;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.service.EarEveManageService;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import com.css.fxfzxczh.util.UUIDGenerator;
import com.css.fxfzxczh.web.PlatformSessionContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:
 * @CreateTime: 2024/3/22 9:29
 * @Description:
 */
@Service
public class InstrumentIntensityServiceImpl implements InstrumentIntensityService {

    @Autowired
    InstrumentIntensityExcelVerifyHandler excelVerifyHandler;

    @Resource
    InstrumentIntensityRepository instrumentIntensityRepository;

    @Resource
    EarEveManageService earEveManageService;

    @Override
    public Map<String, Object> getPage(int curPage, int pageSize,String xsemId) {
        Map<String,Object> page=instrumentIntensityRepository.getPage(curPage,pageSize,xsemId);
        return page;
    }

    @Override
    public void importExcel(MultipartFile file) throws Exception {
        // 导入的参数信息
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(4);
        params.setNeedVerify(true);
        params.setVerifyHandler(excelVerifyHandler);//校验

        InputStream inputStream = file.getInputStream();
        ExcelImportResult<ImportExcelVo> result = ExcelImportUtil.importExcelMore(inputStream, ImportExcelVo.class, params);
        List<ImportExcelVo> failList = result.getFailList();
        if (PlatformObjectUtils.isNotEmpty(failList)) {
            StringBuilder errorMsg = new StringBuilder("<font size='5px' color='red'>导入失败：</font>");
            for (ImportExcelVo importExcelVo : failList) {
                errorMsg.append("<font size='4px' ><br>").append("第").append(importExcelVo.getRowNum() + 1).append("行： ").append(importExcelVo.getErrorMsg()).append("</font>");
            }
            throw new RuntimeException(errorMsg.toString());
        }
        List<ImportExcelVo> list = result.getList();

        List<InstrumentIntensity> instrumentIntensityList = new ArrayList<>();
        String userID = PlatformSessionContext.getUserID();
        EarEveManage currentEvent = earEveManageService.getCurrentEvent();
        String xsemId = currentEvent.getXsemId();
//        String userID="123";
//        String xsemId="456";
        for (ImportExcelVo importExcelVo : list) {
            InstrumentIntensity instrumentIntensity=new InstrumentIntensity();
            BeanUtils.copyProperties(importExcelVo, instrumentIntensity);
            instrumentIntensity.setId(UUIDGenerator.getUUID());
            instrumentIntensity.setCreateUser(userID);
            instrumentIntensity.setCreateTime(new Date());
            instrumentIntensity.setDelFlag(YNEnum.N.toString());
            instrumentIntensity.setXsemId(xsemId);
            instrumentIntensity.setGeom("POINT("+importExcelVo.getStationLongitude()+" "+importExcelVo.getStationLatitude()+")");
            instrumentIntensityList.add(instrumentIntensity);
        }
        instrumentIntensityRepository.batchSave(instrumentIntensityList);
        System.out.println(list);


    }

    @Override
    public void delete(List<String> ids) {
        instrumentIntensityRepository.delete(ids);
    }

    @Override
    public List<InstrumentIntensity> getInstrumentIntensityList(String xsemId) {
        return instrumentIntensityRepository.getInstrumentIntensityList(xsemId) ;
    }
}
